home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / util / cli / sect.lha / Sect / sect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-07  |  4.5 KB  |  147 lines

  1.  
  2. /*************************************************/
  3. /* NAME: sect                                    */
  4. /*                                               */
  5. /* AUTHOR: Jascha Franklin-Hodge                 */
  6. /*         joeshmoe@world.std.com                */
  7. /*                                               */
  8. /* PURPOSE: To take a group of lines             */
  9. /*          out of a file (or stdin)             */
  10. /*          and print them to stdout             */
  11. /*                                               */
  12. /* USAGE: sect [-q] [-v] -s# [-e# | -l#] [file]  */
  13. /*                                               */
  14. /*  -s#   start at line #                        */
  15. /*  -e#   end at line # (superceeds -l#)         */
  16. /*  -l#   take # lines                           */
  17. /*  -v    takes all lines EXCEPT those           */
  18. /*        specified                              */
  19. /*  -q    supress error messages                 */
  20. /*                                               */
  21. /* LAST MODIFIED: 01/01/95                       */
  22. /*************************************************/
  23.  
  24. #define TRUE -1
  25. #define FALSE 0
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29.  
  30. FILE *input_file;     /*-* File to read from                              *-*/
  31.  
  32. char *program_name;   /*-* the name of the program. (Usually 'sect')      *-*/
  33. int start_line=0;     /*-* Line to start cutting at                       *-*/
  34. int end_line=0;       /*-* Line to end cutting at                         *-*/
  35. int from_stdin=FALSE; /*-* Is data from stdin rather then a file?         *-*/
  36. int invert=FALSE;     /*-* Invert selection?                              *-*/
  37. int quiet=FALSE;      /*-* Supress error messages                         *-*/
  38. int current_line=1;   /*-* Counter: Keeps track of the current line       *-*/
  39. int current_char=0;   /*-* Holds the value for the character being read   *-*/
  40.  
  41. main (int argc, char *argv[]) {
  42.  
  43.   program_name = argv[0]; /*-* store program name for later use *-*/
  44.  
  45. /****** Process Command-Line options ******/
  46. while ( (argc > 1) && (argv[1][0] == '-') && (argv[1][1] != NULL) ) {
  47.   switch (argv[1][1]) {  
  48.  
  49.     case 'q':
  50.       quiet=TRUE;
  51.       break;
  52.  
  53.     case 'v':   /*-* Invert Option                 *-*/
  54.       invert = TRUE;
  55.       break;
  56.  
  57.     case 's':   /*-* Start line                    *-*/
  58.       start_line = atoi (&argv[1][2]);
  59.       if ( start_line < 1 ) {
  60.     if (quiet == FALSE) {
  61.       printf ("ERROR: starting line must be greater than 0.\n");
  62.     }
  63.     usage();
  64.       }
  65.       break;
  66.  
  67.     case 'e':  /*-* End Line                       *-*/
  68.       end_line = atoi (&argv[1][2]);
  69.       if ( end_line < start_line ) {
  70.     if (quiet == FALSE) {
  71.       printf ("ERROR: end line must come after start line.\n");
  72.     }
  73.     usage();
  74.       }       
  75.       break;
  76.  
  77.     case 'l':
  78.       if ( end_line == 0 ) {
  79.     end_line = (start_line + atoi (&argv[1][2]) ) - 1;
  80.     if ( end_line < 1 ) {
  81.       if (quiet == FALSE) {
  82.         printf ("ERROR: number of lines must be greater than 0.\n");
  83.       }
  84.       usage();
  85.     } 
  86.       }
  87.       break;
  88.  
  89.     default:    /*-* Invalid Option                *-*/
  90.       if (quiet == FALSE) printf ("Illegal option -- %c\n",argv[1][1]);
  91.       usage (); /*-* Explain correct usage of sect *-*/
  92.   }
  93. argc--;
  94. argv++;
  95. }
  96.  
  97. /****** Check to make sure all required parameters were specified     ******/
  98. if ( (start_line < 1) || (end_line < 1 ) ) usage ();
  99.  
  100. /****** Determine if input should be read from a file or from stdin.  ******/
  101. /****** If needed, open a file handle (input_file).                   ******/
  102. if ( (argc > 1) && (strcmp ("-",argv[1]) ) ) {
  103.   input_file = fopen (argv[1], "r");
  104.   from_stdin = FALSE;
  105.   if ( input_file == NULL ) {
  106.     if (quiet == FALSE) printf ("ERROR: Cannot open input file (%s)\n",argv[1]);
  107.     usage ();
  108.   }
  109. } else {
  110.   from_stdin = TRUE;
  111. }
  112.  
  113. /****** Main Loop                                                     ******/
  114. while (1) {
  115.   current_char = input_next_char ();     /*-* Input char *-*/
  116.   if ( current_char == EOF ) break;
  117.   if ( invert == TRUE) {          /*-* Determine if char should be printed *-*/
  118.     if ( (current_line > end_line) || (current_line < start_line) ) print_char();
  119.   } else {
  120.     if ( (current_line <= end_line) && (current_line >= start_line) ) print_char();
  121.   }
  122.   if ( current_char == '\n' ) current_line++;  /*-* Increment line counter *-*/
  123. }
  124.  
  125. return (0);
  126. }
  127.  
  128. int print_char () {
  129.       printf ("%c",current_char);
  130. }
  131.  
  132. int input_next_char() {
  133.   if ( from_stdin == TRUE ) {
  134.     return (getchar());
  135.   } else {
  136.     return (fgetc(input_file));
  137.   }
  138. }
  139.  
  140. int usage () {
  141.   if (quiet == FALSE) {
  142.     printf ("Usage: %s [-q] [-v] -s# [-e# | -l#] [file]\n", program_name);
  143.   }
  144.   exit(1);
  145. }
  146.  
  147.